﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using SapB1WebPOS.Utils;
using SapB1WebPOS.DAL;
using System.IO;
using System.Text;

namespace SapB1WebPOS.BLL
{
    public class ARInvoiceBLL
    {
        public DataSet GetDocuments(string companyDB)
        {
            try
            {
                DataSet dt = new DataSet("OINV");
                ARInvoiceDAO connect = new ARInvoiceDAO();
                dt = connect.GetOINV(companyDB);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetAllPayment(string companyDB, string docEntry)
        {
            try
            {
                DataSet dt = new DataSet("ORCT");
                ARInvoiceDAO connect = new ARInvoiceDAO();
                dt = connect.GetAllPayment(companyDB, docEntry);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentByDocEntry(string companyDB, string docEntry)
        {
            try
            {
                DataSet dt = new DataSet("OINV");
                ARInvoiceDAO connect = new ARInvoiceDAO();
                dt = connect.GetOINVByDocEntry(companyDB, docEntry);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentForSearch(string companyDB, string keySearch)
        {
            try
            {
                DataSet dt = new DataSet("OINV");
                ARInvoiceDAO connect = new ARInvoiceDAO();
                dt = connect.GetOINVForSearch(companyDB, keySearch);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentLastRecord(string companyDB)
        {
            try
            {
                DataSet dt = new DataSet("OINV");
                ARInvoiceDAO connect = new ARInvoiceDAO();
                dt = connect.GetOINVLastRecord(companyDB);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentLineByDocEntry(string companyDB, string docEntry)
        {
            try
            {
                DataSet dt = new DataSet("OINV");
                ARInvoiceDAO connect = new ARInvoiceDAO();
                dt = connect.GetINV1ByDocEntry(companyDB, docEntry);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public string CreateReceiptHTML(DataSet dsData, string companyDB, string loginUser, string companyName, string pathHtmlfile)
        {
            try
            {
                double totalQuantity = 0, billTotal = 0, tax = 0, netValue = 0, discount = 0;
                string html = string.Empty;
                SAPUtils sapUtils = new SAPUtils(companyDB);
                using (StreamReader reader = new StreamReader(pathHtmlfile))
                {
                    html = reader.ReadToEnd();
                }
                if (html.Length > 0)
                {
                    // Header
                    html = html.Replace("$COMPANYNAME", companyName);
                    html = html.Replace("$ReceiptType", totalQuantity.ToString());
                    html = html.Replace("$TRXNumber", dsData.Tables[0].Rows[0]["DocEntry"].ToString());
                    html = html.Replace("$TRXDate", DateTime.Parse(dsData.Tables[0].Rows[0]["TaxDate"].ToString()).ToString("MM/dd/yyyy"));
                    html = html.Replace("$CurrentDate", DateTime.Now.Date.ToString("MM/dd/yyyy"));

                    html = html.Replace("$UserDetails", loginUser);
                    html = html.Replace("$CustomerRequiredDetails", totalQuantity.ToString());
                    html = html.Replace("$TransacionItems", totalQuantity.ToString());

                    // Body 
                    html = html.Replace("$PrintFunction", "onload=\"window.print();\"");

                    StringBuilder itemList = new StringBuilder();
                    itemList.Append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");

                    string whsName = string.Empty;
                    foreach (DataRow r in dsData.Tables[1].Rows)
                    {
                        itemList.Append("<tr>");
                        itemList.Append(" <td width=\"25%\">");
                        itemList.Append(r["PriceBefDi"].ToString());
                        itemList.Append("</td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append(r["DiscPrcnt"].ToString());
                        itemList.Append("</td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append(r["LineTotal"].ToString());
                        itemList.Append("</td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append("<div align=\"right\">");
                        itemList.Append(r["LineTotal"].ToString());
                        itemList.Append("</div>");
                        itemList.Append("</td>");
                        itemList.Append("</tr>");
                        totalQuantity += double.Parse(r["Quantity"].ToString());
                    }
                    itemList.Append(" </table>");
                    // Item List
                    itemList = new StringBuilder();
                    itemList.Append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-top:5px;\">");
                    foreach (DataRow r in dsData.Tables[1].Rows)
                    {
                        itemList.Append("<tr>");
                        itemList.Append(" <td colspan=\"4\">");
                        itemList.Append(r["ItemCode"].ToString() + " - " + r["Dscription"].ToString());
                        itemList.Append("</td>");
                        itemList.Append("</tr>");
                        itemList.Append("<tr style=\"height:5px;\">");
                        itemList.Append("<td width=\"25%\"></td>");
                        itemList.Append("<td width=\"25%\"></td>");
                        itemList.Append("<td width=\"25%\"></td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append("<div align=\"right\"></div>");
                        itemList.Append("</td>");
                        itemList.Append(" </tr>");
                        itemList.Append("<tr>");
                        itemList.Append(" <td width=\"25%\">");
                        itemList.Append(sapUtils.FormatNumeric(r["Quantity"].ToString(), SAPUtils.QTYDEC));
                        itemList.Append("</td>");
                        itemList.Append(" <td width=\"25%\">");
                        itemList.Append(sapUtils.FormatNumeric(r["PriceBefDi"].ToString(), SAPUtils.PRICEDEC));
                        itemList.Append("</td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append(sapUtils.FormatNumeric(r["DiscPrcnt"].ToString(), SAPUtils.PERCENTDEC));
                        itemList.Append("</td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append("<div align=\"right\">");
                        itemList.Append(sapUtils.FormatNumeric(r["LineTotal"].ToString(), SAPUtils.PRICEDEC));
                        itemList.Append("</div>");
                        itemList.Append("</td>");
                        itemList.Append("</tr>");
                        itemList.Append("<tr style=\"height:10px;\">");
                        itemList.Append("<td width=\"25%\"></td>");
                        itemList.Append("<td width=\"25%\"></td>");
                        itemList.Append("<td width=\"25%\"></td>");
                        itemList.Append("<td width=\"25%\">");
                        itemList.Append("<div align=\"right\"></div>");
                        itemList.Append("</td>");
                        itemList.Append(" </tr>");

                        billTotal += double.Parse(r["LineTotal"].ToString());
                        tax += double.Parse(r["VatSum"].ToString());

                        double disCountAmt = (double.Parse(r["LineTotal"].ToString()) * double.Parse(r["DiscPrcnt"].ToString())) / 100;
                        totalQuantity += double.Parse(r["Quantity"].ToString());
                        discount += disCountAmt;
                        whsName = r["WhsName"].ToString();

                    }
                    html = html.Replace("$Warehouse", whsName);
                    netValue = billTotal + tax;
                    itemList.Append(" </table>");
                    html = html.Replace("$TRXList", itemList.ToString());

                    // Footer
                    html = html.Replace("$TotalQuantity", totalQuantity.ToString());
                    html = html.Replace("$BillTotal", sapUtils.FormatNumeric(billTotal.ToString(), SAPUtils.PRICEDEC));
                    html = html.Replace("$Tax", sapUtils.FormatNumeric(tax.ToString(), SAPUtils.PRICEDEC));
                    html = html.Replace("$NetValue", sapUtils.FormatNumeric(netValue.ToString(), SAPUtils.PRICEDEC));
                    NumberToEnglish num = new NumberToEnglish();
                    while (true)
                    {
                        String input = Console.ReadLine();
                        if (input == null || input.Length == 0) break;
                        Console.WriteLine(num.changeNumericToWords(input));
                        Console.WriteLine();
                    }

                    html = html.Replace("$AmountText", num.changeCurrencyToWords(netValue.ToString(), ""));
                    html = html.Replace("$TRXBarCodeNumber", dsData.Tables[0].Rows[0]["DocEntry"].ToString());

                    return html;
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return "<b>Error: " + ex.Message + "</b>";
            }
            return "<b>Error: Can not read templete file.<b>";
        }
    }
}